<template>
	<view class="content">
		<view class="info" v-if="titleIndex==1">
		<!-- 	<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">旧密码</view>
				<input type="password" placeholder="请输入旧密码" v-model="oldPassword" />
			</view> -->
			<view class="item">
				<image src="@/static/img/forgot-01.png" mode="widthFix"></image>
				<view class="text">手机号</view>
				<input type="text" placeholder="请输入新手机号" disabled="true" class="col_9" v-model="phone" />
			</view>
			<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">验证码</view>
				<input type="text" placeholder="" v-model="code" />
				<text v-if="!istime" class="get get_btn" @click="get()" id="code_text">获取验证码</text>
				<text v-if="istime" class="get" id="code_text">{{countdown}}s</text>
			</view>
			<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">新密码</view>
				<input type="password" placeholder="请输入新密码" v-model="newPassword" />
			</view>
			<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">确认密码</view>
				<input type="password" placeholder="请再次输入新密码" v-model="ageinPassword" />
			</view>
		</view>
		<view class="info" v-if="titleIndex==2">
			<!-- <view class="item">
				<image src="@/static/img/forgot-03.png" mode="widthFix"></image>
				<view class="text">原邮箱</view>
				<input type="text" placeholder="请输入原邮箱" v-model="oldPassword" />
			</view> -->
			<view class="item">
				<image src="@/static/img/forgot-03.png" mode="widthFix"></image>
				<view class="text">新邮箱</view>
				<input type="text" placeholder="请输入新邮箱" v-model="newEmail" />
			</view>
			<!-- <view class="item">
				<image src="@/static/img/forgot-03.png" mode="widthFix"></image>
				<view class="text">邮箱验证码</view>
				<input v-if="!true" type="text" placeholder="" v-model="ageinPassword" />
				<text v-if="true" class="get get_btn" @click="get()" id="code_text">获取验证码</text>
				<text v-if="!true" class="get" id="code_text">60s</text>
			</view> -->
		</view>
		<view class="info" v-if="titleIndex==3">
			<view class="item">
				<image src="@/static/img/forgot-01.png" mode="widthFix"></image>
				<view class="text">手机号</view>
				<input type="text" placeholder="请输入新手机号" v-model="newPhone" />
			</view>
			<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">验证码</view>
				<input type="text" placeholder="" v-model="code" />
				<text v-if="!istime" class="get get_btn" @click="get()" id="code_text">获取验证码</text>
				<text v-if="istime" class="get" id="code_text">{{countdown}}s</text>
			</view>
		</view>
		<view class="hint" v-if="titleIndex==4">
			已设置二级密码，若您希望重新设置二级密码，请输入以下信息：
		</view>
		<view class="info" v-if="titleIndex==4">
			<!-- <view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">旧密码</view>
				<input type="password" placeholder="请输入二级密码" v-model="oldPayPassword" />
			</view> -->
			<view class="item">
				<image src="@/static/img/forgot-01.png" mode="widthFix"></image>
				<view class="text">手机号</view>
				<input type="text" placeholder="请输入新手机号" disabled="true" class="col_9" v-model="phone" />
			</view>
			<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">验证码</view>
				<input type="text" placeholder="" v-model="code" />
				<text v-if="!istime" class="get get_btn" @click="get()" id="code_text">获取验证码</text>
				<text v-if="istime" class="get" id="code_text">{{countdown}}s</text>
			</view>
			<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">新密码</view>
				<input type="password" placeholder="请输入二级密码" v-model="newPayPassword" />
			</view>
			<view class="item">
				<image src="@/static/img/sign-info-02.png" mode="widthFix"></image>
				<view class="text">确认密码</view>
				<input type="password" placeholder="请再次确认密码" v-model="ageinPayPassword" />
			</view>
		</view>
		<view class="default_btn" @click="save">保存</view>
	</view>
</template>

<script>
	import {
		editUserInfo,
		userCode,
		editPhone,
		editPassword
	} from '@/common/js/index.js'
	export default {
		data() {
			return {
				oldPassword: '',
				newPassword: '',
				ageinPassword: '',
				titleIndex: '',
				newEmail: '',
				newPhone: '',
				oldPayPassword: '',
				newPayPassword: '',
				ageinPayPassword: '',
				countdown: 120,
				timer: '',
				istime: false,
				code: '',
				phone: '',
			}
		},
		onLoad(e) {
			console.log(e);
			this.titleIndex = e.num
			this.phone = e.phone
			var index = e.num
			// this.navIndex = e.num
			if (index == 1) {
				uni.setNavigationBarTitle({
					title: '修改登录密码'
				});
			} else if (index == 2) {
				uni.setNavigationBarTitle({
					title: '修改绑定邮箱'
				});
				this.newEmail = e.email
			} else if (index == 3) {
				uni.setNavigationBarTitle({
					title: '修改绑定手机'
				});
			} else if (index == 4) {
				uni.setNavigationBarTitle({
					title: '修改二级密码'
				});
			}
		},
		methods: {
			save(e) {
				var info
				if (this.titleIndex == 1) {
					var hint
					if ( !this.code) {
						hint = '请输入验证码'
					
					} else if (!hint && !this.newPassword) {
						hint = '请输入新密码'
					} else if (!hint && !this.ageinPassword) {
						hint = '请再次输入新密码'
					} else if (!hint && this.newPassword != this.ageinPassword) {
						hint = '两次密码不一致，请重新输入'
					}
					if (hint) {
						uni.showToast({
							title: hint,
							icon: 'none',
							duration: 3000
						})
						return
					} else {
						info = {
							// mobile: this.newPhone,
							code: this.code,
							user_pwd: this.newPassword
							// user_pwd_old: this.oldPassword,
						}
					}
				} else if (this.titleIndex == 2) {
					var hint
					if (!this.newEmail) {
						hint = '请输入新邮箱'
					}
					if (hint) {
						uni.showToast({
							title: hint,
							icon: 'none',
							duration: 3000
						})
						return
					} else {
						info = {
							email: this.newEmail
						}
					}
				} else if (this.titleIndex == 3) {
					var hint
					if (!this.newPhone) {
						hint = '请输入新手机号'
					}
					if (hint) {
						uni.showToast({
							title: hint,
							icon: 'none',
							duration: 3000
						})
						return
					} else {
						info = {
							mobile: this.newPhone,
							code: this.code
						}
					}
				} else if (this.titleIndex == 4) {
					var hint
					if ( !this.code) {
						hint = '请输入验证码'
					}else if (!hint && !this.newPayPassword) {
						hint = '请输入新密码'
					} else if (!hint && !this.ageinPayPassword) {
						hint = '请再次输入新密码'
					} else if (!hint && this.newPayPassword != this.ageinPayPassword) {
						hint = '两次密码不一致，请重新输入'
					}
					if (hint) {
						uni.showToast({
							title: hint,
							icon: 'none',
							duration: 3000
						})
						return
					} else {
						info = {
							// paypassword_old: this.oldPayPassword,
							// mobile: this.newPhone,
							code: this.code,
							paypassword: this.ageinPayPassword
						}
					}
				}
				if (this.titleIndex == 3) {
					editPhone(info).then(res=>{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						if (res.code == 200) {
							setTimeout(function() {
								uni.navigateBack()
							}, 2000)
						}
					})
				} else if (this.titleIndex == 2){
					editUserInfo(info).then(res => {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						if (res.code == 200) {
							setTimeout(function() {
								uni.navigateBack()
							}, 2000)
						}
					})
				} else if (this.titleIndex == 1||this.titleIndex == 4){
					editPassword(info).then(res => {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						if (res.code == 200) {
							setTimeout(function() {
								uni.navigateBack()
							}, 2000)
						}
					})
				}
			},
			get() {
				userCode({
					mobile: this.titleIndex==3? this.newPhone:this.phone
				}).then(res => {
					console.log(res);
					if (res.code == 200) {
						this.istime = true
						this.timer = setInterval(() => {
							if (this.countdown > 0) {
								this.countdown--;
							} else {
								this.istime = false
								clearInterval(this.timer)
							}
						}, 1000)
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 2000,
						});
					}
				})
			},
		},
	}
</script>

<style scoped lang="scss">
	.content {
		/* background: white; */
		position: relative;
		overflow: hidden;
	}

	.info {
		margin: 40rpx 30rpx 200rpx;
		padding: 0 30rpx 88rpx;
		background-color: white;
		background: #FFFFFF;
		border-radius: 12rpx;
		overflow: hidden;

		.item {
			height: 118rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #EEEEEE;

			image {
				width: 48rpx;
			}

			.text {
				flex: 1;
				margin-left: 16rpx;
			}

			input {
				font-size: 26rpx;
				text-align: right;
			}

			.get {
				height: 52rpx;
				line-height: 52rpx;
				padding: 0 20rpx;
				border-radius: 25rpx;
				font-size: 24rpx;
				margin-left: 20rpx;
				background-color: #FF524F;
				color: white;
			}
		}
	}

	.hint {
		color: #BC7A25;
		line-height: 48rpx;
		margin: 40rpx 30rpx;
	}
	.col_9 {
		color: #999999;
	}
</style>